﻿function TweetCraft.MinimapButton_MouseDown(self)
	-- Remember where the cursor was in case the user drags
	
	local vCursorX, vCursorY = GetCursorPosition()
	
	vCursorX = vCursorX / self:GetEffectiveScale()
	vCursorY = vCursorY / self:GetEffectiveScale()
	
	TweetCraftMinimapButton.CursorStartX = vCursorX
	TweetCraftMinimapButton.CursorStartY = vCursorY
	
	local vCenterX, vCenterY = TweetCraftMinimapButton:GetCenter()
	local vMinimapCenterX, vMinimapCenterY = Minimap:GetCenter()
	
	TweetCraftMinimapButton.CenterStartX = vCenterX - vMinimapCenterX
	TweetCraftMinimapButton.CenterStartY = vCenterY - vMinimapCenterY
	
	TweetCraftMinimapButton.EnableFreeDrag = IsModifierKeyDown()
end

function TweetCraft.MinimapButton_DragStart(self)
	MCSchedulerLib:ScheduleUniqueRepeatingTask(0, TweetCraft.MinimapButton_UpdateDragPosition, self)
end

function TweetCraft.MinimapButton_DragEnd(self)
	MCSchedulerLib:UnscheduleTask(TweetCraft.MinimapButton_UpdateDragPosition, self)
end

function TweetCraft.MinimapButton_UpdateDragPosition(self)
	-- Remember where the cursor was in case the user drags
	
	local vCursorX, vCursorY = GetCursorPosition()
	
	vCursorX = vCursorX / self:GetEffectiveScale()
	vCursorY = vCursorY / self:GetEffectiveScale()
	
	local vCursorDeltaX = vCursorX - TweetCraftMinimapButton.CursorStartX
	local vCursorDeltaY = vCursorY - TweetCraftMinimapButton.CursorStartY
	
	--
	
	local vCenterX = TweetCraftMinimapButton.CenterStartX + vCursorDeltaX
	local vCenterY = TweetCraftMinimapButton.CenterStartY + vCursorDeltaY
	
	if TweetCraftMinimapButton.EnableFreeDrag then
		TweetCraft.MinimapButton_SetPosition(vCenterX, vCenterY)
	else
		-- Calculate the angle and set the new position
		local vAngle = math.atan2(vCenterX, vCenterY)
		
		TweetCraft.MinimapButton_SetPositionAngle(vAngle)
	end
end

function TweetCraft:RestrictAngle(pAngle, pRestrictStart, pRestrictEnd)
	if pAngle <= pRestrictStart
	or pAngle >= pRestrictEnd then
		return pAngle
	end
	
	local vDistance = (pAngle - pRestrictStart) / (pRestrictEnd - pRestrictStart)
	
	if vDistance > 0.5 then
		return pRestrictEnd
	else
		return pRestrictStart
	end
end

function TweetCraft.MinimapButton_SetPosition(pX, pY)
	tweetCraftOptions.MinimapButtonAngle = nil
	tweetCraftOptions.MinimapButtonX = pX
	tweetCraftOptions.MinimapButtonY = pY
	
	TweetCraftMinimapButton:SetPoint("CENTER", Minimap, "CENTER", pX, pY)
end

function TweetCraft.MinimapButton_SetPositionAngle(pAngle)
	local vAngle = pAngle
	
	-- Restrict the angle from going over the date/time icon or the zoom in/out icons
	--[[
	local vRestrictedStartAngle = nil
	local vRestrictedEndAngle = nil
	
	if GameTimeFrame:IsVisible() then
		if MinimapZoomIn:IsVisible()
		or MinimapZoomOut:IsVisible() then
			vAngle = Outfitter:RestrictAngle(vAngle, 0.4302272732931596, 2.930420793963121)
		else
			vAngle = Outfitter:RestrictAngle(vAngle, 0.4302272732931596, 1.720531504573905)
		end
		
	elseif MinimapZoomIn:IsVisible()
	or MinimapZoomOut:IsVisible() then
		vAngle = Outfitter:RestrictAngle(vAngle, 1.720531504573905, 2.930420793963121)
	end
	
	-- Restrict it from the tracking icon area
	
	vAngle = Outfitter:RestrictAngle(vAngle, -1.290357134304173, -0.4918423429923585)
	]]--
	
	--
	
	local vRadius = 80
	
	vCenterX = math.sin(vAngle) * vRadius
	vCenterY = math.cos(vAngle) * vRadius
	
	TweetCraftMinimapButton:SetPoint("CENTER", Minimap, "CENTER", vCenterX - 1, vCenterY - 1)
	
	tweetCraftOptions.MinimapButtonAngle = vAngle
end

function TweetCraft.MinimapButton_MouseUp()
	TweetCraft.ToggleTweetCraft()
end